package com.svmc.app.task.alarm;

import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Window;

import com.svmc.app.task.activity.R;
import com.svmc.app.task.adapter.DbAdapter;
import com.svmc.app.task.model.TaskModel;
import com.svmc.app.task.utils.CommonDefine;
import com.svmc.app.task.utils.UtilString;
import com.svmc.app.task.view.CustomDialog;

public class DialogAlarm extends Activity implements CommonDefine{
	String path ;
	TaskModel task;
	int id_task;
	public static MediaPlayer mediaPlayer;
	CustomDialog mydialog;
	Vibrator vibrator;
	UtilString utils;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setBackgroundDrawable(
				new ColorDrawable(Color.argb(0, 0, 0, 0)));
		
		Bundle bundle = getIntent().getExtras();
		id_task = (int) bundle.getLong(ALARM_SERVICE_ID_TASK);
		//path = bundle.getString(ALARM_SERVICE_PATH_RINGTONE);
		mydialog = new CustomDialog(this);
		
		mediaPlayer = new MediaPlayer();
		task = new TaskModel();
		utils =new UtilString();
	}
	@Override
	protected void onResume() {
		super.onResume();
		DbAdapter db = new DbAdapter(this);
		task = db.getTaskById(id_task);
		path = task.getPathRingtone();
		
		mydialog.setDialogType(DIALOG_ALARM, task.getTask(), task.getNote(), task.getTimefinish());
		//mydialog.setDialogType(DIALOG_ALARM, "Canh bao", path, "21/09/1989");
		mydialog.show();
		System.out.println("Path ring"+ path);
		playSong(utils.getPathRing(path));
	}
	
	public  void playSong (String pathSong){
		
		try {
			vibrator = (Vibrator)getSystemService(this.VIBRATOR_SERVICE);
			vibrator.vibrate(new long[] { 0, 0, 500 }, 1);
			
			final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
			if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
				mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
				if(utils.getPathRing(path).equalsIgnoreCase("2131034112")){
					mediaPlayer = MediaPlayer.create(this, R.raw.sound_default);
					mediaPlayer.setLooping(true);
					mediaPlayer.setVolume(100, 100);
					mediaPlayer.start();
				}else {
					mediaPlayer.setDataSource(pathSong);
					mediaPlayer.prepare();
					mediaPlayer.setLooping(true);
					mediaPlayer.setVolume(100, 100);
					mediaPlayer.start();
				}
				
			}
			
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}
	public static void stopAudioAlarm (){
		if(mediaPlayer.isPlaying()){
			mediaPlayer.stop();
		}
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		
		super.onDestroy();
		 if (vibrator != null)
			 vibrator.cancel();
		task =null;
		mediaPlayer=null;
		System.gc();
	}

}
